﻿using IM.Easy.Core.Enums;
using IM.Easy.Core.Log;
using IM.Easy.Core.Result;
using IM.Easy.Core.Utils;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IM.Easy.Core.Filters
{
    public class ApiExceptionFilter : IExceptionFilter
    {
        private readonly IExceptionLogger _logger;
        public ApiExceptionFilter(IExceptionLogger logger) { }
        public void OnException(ExceptionContext context)
        {


            ApiResult result;
            var exception = context.Exception;
            //context.HttpContext.RequestServices.GetService(typeof())
            var type = context.Exception.GetType();
            var statusCode = ExceptionUtils.GetStatusCode(type);

            if (statusCode == ResultCode.BusinessLogicError)
            {
                result = ApiResult.Failure(exception.Message, statusCode);
            }
            else
            {
                result = ApiResult.Failure("未知错误", statusCode);
            }
        }
    }
}
